package com.atguigu.gmall.all.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.all.service.ItemService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.client.ItemFeignClient;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.*;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

@Controller
@Api("前台页面")
public class ItemController {

    @Autowired
    private ItemFeignClient itemFeignClient;

    @Autowired
   private ItemService itemService;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private ListFeignClient listFeignClient;
    @GetMapping("{skuId}.html")
    public String getItem(@PathVariable Long skuId, Model model){
        Map item = itemFeignClient.getItem(skuId);
        model.addAllAttributes(item);
        return "item/index";
    }

    @GetMapping("/")
    public String getBaseCategoryList(Model model){
//        List<Map> baseCategoryList = itemService.getBaseCategoryList();
//        model.addAttribute("list",baseCategoryList);

        return "index";
    }
    //页面静态化
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() throws FileNotFoundException {
        List<Map> baseCategoryList = itemService.getBaseCategoryList();
        Context context = new Context();
        context.setVariable("list",baseCategoryList);
        PrintWriter printWriter = new PrintWriter("D:\\index.html");
        templateEngine.process("index/index.html",context,printWriter);
        return Result.ok();
    }

    @GetMapping("/list.html")
    //搜索功能
    public String getSearchList(SearchParam searchParam,Model model){
        SearchResponseVo searchResponseVo = listFeignClient.getSearchList(searchParam);
        //关键词
        model.addAttribute("searchParam",searchParam);

        //品牌数据
        List<SearchResponseTmVo> trademarkList = searchResponseVo.getTrademarkList();
        model.addAttribute("trademarkList",trademarkList);
        //平台属性
        List<SearchResponseAttrVo> attrsList = searchResponseVo.getAttrsList();
        model.addAttribute("attrsList",attrsList);

        //商品数据
        List<Goods> goodsList = searchResponseVo.getGoodsList();
        model.addAttribute("goodsList",goodsList);
        //分页
        model.addAttribute("pageNo",searchResponseVo.getPageNo());
        model.addAttribute("totalPages",searchResponseVo.getTotalPages());

        //全部结果数据
        //1品牌
        String trademark = searchParam.getTrademark();
        if(trademark!=null){
            String[] t = trademark.split(":");
            model.addAttribute("trademarkParam",t[1]);
        }
        //构建urlParam
        String urlParams = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParams);
        //2.平台属性
        String[] props = searchParam.getProps();
        if(props!=null){
            List<Map> listMap = new ArrayList<>();
            for (String prop : props) {
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                map.put("attrId",p[0]);
                listMap.add(map);
            }
            model.addAttribute("propsParamList",listMap);
        }
        //排序
        String order = searchParam.getOrder();
        System.out.println(order);
        Map orderMap = new HashMap();
        if(!StringUtils.isEmpty(order)){
            String[] r = order.split(":");
            orderMap.put("type",r[0]);
            orderMap.put("sort",r[1]);
        }else{
            orderMap.put("type","1");
            orderMap.put("sort","desc");
        }
        model.addAttribute("orderMap",orderMap);


        return "list/index";
    }

    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder("/list.html?");
        String keyword = searchParam.getKeyword();
        if(!StringUtils.isEmpty(keyword)){
            sb.append("&keyword="+keyword);
        }
        String[] props = searchParam.getProps();
        if(!StringUtils.isEmpty(props)&&props.length>0){
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }


}
